package com.jiaojiao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiaojiao.reggie.common.R;
import com.jiaojiao.reggie.entity.User;
import com.jiaojiao.reggie.service.UserService;
import com.jiaojiao.reggie.utils.SendMailUtils;
import com.jiaojiao.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private SendMailUtils sendMailUtils;

    @Autowired
    private ValidateCodeUtils validateCodeUtils;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)){
            //生成随机的4位验证码
            String code = validateCodeUtils.generateValidateCode(4,phone);
            log.info(code);
            //调用阿里云提供的短信服务api完成短信发送
            sendMailUtils.sendMail(code);//邮箱验证
            //需要将生成的验证码保存到Session
            session.setAttribute(phone,code);
            return R.success("短信发送成功");
        }
        return R.error("发送失败");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){
        //获取手机号，验证码，以及生成的验证码
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        //从redis中获取保存的验证码
        String cacheCode = validateCodeUtils.get(phone);
        log.info("手机号：{}，在redis中的验证码为：{}",phone,cacheCode);

        if (cacheCode == null) {
            return R.error("验证码已过期！！！");
        }
        if (code.equals(cacheCode)) {
            //如果能够比对成功，说明登录成功
            //判断当前手机号对应的用户是否为新用户，如果是新用户就自动完成注册
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getPhone, phone);
            User user = userService.getOne(lqw);
            if (user == null) {
                user = new User();
                user.setPhone(phone);
                user.setName("新用户："+phone);
                userService.save(user);
            }
            //将用户id保存到session中，供过滤器检测用户是否登录
            session.setAttribute("user", user.getId());
            return R.success(user);
        }

//        Object codeInSession = session.getAttribute(phone);
        //页面提交啊的验证码和session的验证码对比,现在是缓存
//        if (codeInSession != null && codeInSession.equals(code)) {
//            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(User::getPhone,phone);
//            User user = userService.getOne(queryWrapper);
//            if (user == null){
//                //用户为新用户
//                user = new User();
//                user.setPhone(phone);
//                userService.save(user);
//            }
//            session.setAttribute("user",user.getId());
//            return R.success(user);
//        }
        return R.error("登录失败啦");
    }
}
